package top.soulblack.purchasesale.service;


import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Store;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.common.enums.ResultEnum;
import top.soulblack.purchasesale.exception.PurchaseException;
import top.soulblack.purchasesale.mapper.UserMapper;
import top.soulblack.purchasesale.model.StoreHouse;
import top.soulblack.purchasesale.model.User;

import java.math.BigInteger;
import java.util.List;

/**
 * @Author: 韦溜溜
 * @Description:用户服务实现类
 * @Date: Create in 16:09 2019/5/19
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StoreHouseService storehouseService;

    public List<User> findAll() {
        return userMapper.selectList();
    }

    public List<User> findByRoleId( Integer roleId) {
     return userMapper.selectByRoleId(roleId);
    }

    public List<User> selectForShop(String shop) {
        return userMapper.selectForshop(shop);
    }

    public boolean addUser(User user) {
        if(userMapper.addUser(user)==1)
            return true;
        return false;
    }

    public boolean existUser(String account) {
        int num  = userMapper.existUser(account);
        if (num == 1) {
            return true;
        }
        return false;
    }

    public boolean deleteUser(Integer UserId) {
        int num = userMapper.deleteUser(UserId);
        if (num == 1) {
            return true;
        }
        return false;
    }

    public boolean updateUser(User user) {
        int num = userMapper.updateUser(user);
        if (num == 1) {
            return true;
        }
        return false;
    }

    public User selectByOpenid( String openid){
        User user = userMapper.selectByOpenid(openid);
        if(user==null){
            log.error("【查询用户】通过openid查询,openid{}",openid);
            throw new PurchaseException(ResultEnum.USEREMPITY);
        }
        return user;
    }

    public User selectByUserName(String userName){
        User user = userMapper.selectByOpenid(userName);
        if(user==null){
            log.error("【查询用户】通过用户名查询,{}",userName);
            throw new PurchaseException(ResultEnum.USEREMPITY);
        }
        return user;
    }
}

